package com.bubblegumproject.ogo.swing.events;

import java.io.Serializable;
import java.util.Map;
import java.util.HashMap;

/**
 * An ErrorEvent is a high-level representation of an error that occurred during the processing of an application.
 *
 * @author Azubuko Obele (buko.obele@gmail.com)
 */
public class ErrorEvent implements Serializable {

    private Throwable cause;

    private Object source;

    private Map<String, Object> state = new HashMap<String, Object>();

    public ErrorEvent(Object source, Throwable cause) {
        this.source = source;
        this.cause = cause;
    }

    public Throwable getCause() {
        return cause;
    }

    public Object getSource() {
        return source;
    }

    public Object get(String key) {
        assert(key != null);
        return state.get(key);
    }

    public void set(String key, Object value) {
        assert(key != null);
        state.put(key, value);
    }

    public boolean is(Class<? extends Throwable> errorClass) {
        Throwable t = this.cause;
        while (t != null) {
            if (errorClass.isInstance(t)) return true;
            t = t.getCause();
        }
        return false;
    }

    public String toString() {
        return "[" + getClass().getSimpleName() +" source=" + source + " cause=" + cause + " state=" + state + "]";
    }
}
